//
//  90. 子集II.swift
//  LeetCodeTrain
//
//  Created by rjb on 2021/6/29.
//  Copyright © 2021 rjb. All rights reserved.
//

import Foundation

class Solution90 {
    var result: [[Int]] = [[]]
    var path: [Int] = []
    func subsetsWithDup(_ nums: [Int]) -> [[Int]] {
        // 去重就得排序，子集是收集有节点的值
        let sortNums = nums.sorted()
        backTrace(sortNums, start: 0)
        return result
    }
    func backTrace(_ nums: [Int], start: Int) {
        for i in start..<nums.count {
            let num = nums[i]
            if i > start && num == nums[i - 1] {
                continue
            }
            path.append(nums[i])
            result.append(path)
            backTrace(nums, start: i + 1)
            path.removeLast()
        }
    }
    static func test(){
        let solution = Solution90()
        let nums = [1,2,2]
        let result = solution.subsetsWithDup(nums)
        print(result)
    }
}
